
export const listCtrl = {
  watch: {
    list: {
      immediate: true,
      handler(v) {
        this._initSelfList(v)
      }
    }
  },
  data() {
    return {
      self_id: 1, // 列表内部维护的id
      self_list: null, // 内部列表
      ImageIndexArr: [2,6,7,11],
      disableFieldMap: { // 根据媒体类型区分那些表单需要禁用
        '1': ['picturePlay', 'textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour'], // 视频类 
        '3': ['picturePlay', 'textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour'], // 视频类 
        '9': ['picturePlay', 'textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'times'], // 视频类 
        '10': ['picturePlay', 'textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'times'], // 视频类 
        '4': ['picturePlay', 'vol', 'times'], // 文本类 
        '5': ['picturePlay', 'vol', 'times'], // 文本类 
        '8': ['picturePlay', 'vol', 'times'], // 文本类 
        '2': ['textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'vol', 'times'], // 图片类 
        '6': ['textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'vol', 'times'], // 图片类 
        '7': ['textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'vol', 'times'], // 图片类 
        '11': ['textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'vol', 'times'], // 图片类 
        '12': ['textPlay', 'playVelocity', 'typeface', 'typefaceSize', 'typefaceColour', 'times'] // 文件夹
      },
      mediaTypeMap: {
        'Main': [1,9,10,2,6,7,11,12], //主播区
        'Mixed': [1,9,10,2,6,7,11,12], //综合区
        'ImageText': [2,5,6,7,8,11], //图文区
        'Web': [], //网页区
        'Scroll': [5,8], //滚动区
        'Audio': [3], //音频区
      }
    }
  },
  methods: {
    _initSelfList(v) {
      if (!v) {
        this.self_list = null
      } else {
        this.self_list = this._wrapFileInfo(v)
        if (this.self_list[0] && Number(this.self_list[0].mode) > -1) { // 初始化设置播放模式
          this.mode = this.self_list[0].mode
        }
      }
    },
    isDisableField(row, prop) {
      const disableField = this.disableFieldMap[row.mediaType]
      if (!disableField) return false
      return disableField.indexOf(prop) > -1
    },
    // 秒转[HH, mm, ss]
    _s2Hms(s) {
      const oneDay = 60 * 60 * 24
      if (s >= oneDay) s -= oneDay
      let m = ~~(s / 60)
      if (m < 1) {
        return ['00', '00', `${s > 9 ? s : '0' + s}`]
      } else {
        s = s % 60
        const H = ~~(m / 60)
        if (H < 1) {
          return ['00', `${m > 9 ? m : '0' + m}`, `${s > 9 ? s : '0' + s}`]
        } else {
          m = m % 60
          return [`${H > 9 ? H : '0' + H}`, `${m > 9 ? m : '0' + m}`, `${s > 9 ? s : '0' + s}`]
        }
      }
    },
    _Hms2s(hms) {
      const [H, m, s] = hms
      return (Number(H) * 60 * 60) + (Number(m) * 60) + Number(s)
    },

    // 将媒体文件信息包装成列表所需
    _wrapFileInfo(info) {
      if (!info) return info
      if (info instanceof Array) {
        return info.map(element => {
          return this._wrapFileInfo(element)
        });
      }
      let out
      if (info.fileName) { // 暂时用fileName是否存在来区分此信息是否是后台来的数据
        out = {
          ...info,
          startDate: info.startDate ? info.startDate.split(':') : ['00', '00', '00'],
          endDate: info.endDate ? info.endDate.split(':') : ['23', '59', '59']
        }
      } else {

        if (info.duration > 0) {
          info.times = 0
        } else {
          info.times = 1
        }

        let default_duration = 0
        if (this.ImageIndexArr.indexOf(Number(info.type)) > -1) {
          default_duration = 10
          info.times = 0
        }
        let _vol = 50
        if (info.sound || info.sound === 0) {
          _vol = info.sound
        }

        out = {
          mediaId: info.id,
          mediaType: info.type,
          fileSize: info.length,
          fileName: info.name,
          duration: Number(info.duration) || default_duration,
          durationPerPage: 1,
          times: info.times,
          picturePlay: 0,
          textPlay: 0,
          playVelocity: 1,
          typeface: 0,
          typefaceSize: 48,
          typefaceColour: '#ffffff',
          vol: _vol,
          elecFenceId: [],
          monopolize: 0,
          aspect: false,
          _info: {
            ...info
          }
        }
      }
      return this._wrapSelfId(out)
    },
    // 给信息添加self_id
    _wrapSelfId(info) {
      return {
        ...info,
        _id: this.self_id++
      }
    },
    // 给信息添加播放时间,
    _wrapPlayTime(info, mode, preInfo) {
      if (mode !== 1) { // 不是定时播放
        if (info.startDate) return
        this.$set(info, 'startDate', this._s2Hms(0))
        this.$set(info, 'endDate', this._s2Hms(60 * 60 * 24 - 1))
      } else { // 定时播放
        if (!preInfo) { // 为第一媒体
          this.$set(info, 'startDate', info.startDate ? this._s2Hms(this._Hms2s(info.startDate)) : this._s2Hms(0))
          this.$set(info, 'endDate', this._s2Hms(this._Hms2s(info.startDate) + Number(info.duration)))
        } else {
          this.$set(info, 'startDate', this._s2Hms(this._Hms2s(preInfo.endDate) + 1))
          this.$set(info, 'endDate', this._s2Hms(this._Hms2s(info.startDate) + Number(info.duration)))
        }
      }
    },
    // 按模式给列表重置开始与结束时间
    listTimeSet(mode) {
      this.self_list.forEach((element, index, arr) => {
        this._wrapPlayTime(element, mode, arr[index - 1])
      });
      this.self_list = this.self_list.slice()
    },
    // 新增行
    addRow(info) {
      if (!info) return this.$alert('提示', '请选择要添加的媒体')
      // this.$confirm('提示', '确认添加已选中的媒体素材吗？').then((res) => {
      //   if(res){
         
      //   }
      // })
      if (info instanceof Array) {
        this.self_list.push(...this._wrapFileInfo(info))
      } else {
        this.self_list.push(this._wrapFileInfo(info))
      }
      this.listTimeSet(this.mode)
    },
    // 移除行
    removeRow(row) {
      if (!row) return this.$alert('提示', '请选择要移除的媒体')
      this.$confirm('提示', '确认移除已选中的媒体素材吗？').then((res) => {
        if(res){
          if (row instanceof Array) {
            let ids = row.map(item => item._id)
            this.self_list = this.self_list.filter(item => {
              return ids.indexOf(item._id) === -1
            })
          } else {
            let rowindex = this.self_list.findIndex(item => item._id === row._id)
            if (rowindex > -1) {
              this.self_list.splice(rowindex, 1)
            }
          }
          this.listTimeSet(this.mode)
        }
      })
     
    },
    // 上移行
    upRow(row) {
      if (!row) return this.$alert('提示', '请选择要移动的媒体')
      if (row instanceof Array) return this.$alert('提示', '此操作不支持多选')
      let rowindex = this.self_list.findIndex(item => item._id === row._id)
      if (rowindex <= 0) return
      let _copyList = [].concat(this.self_list)
      let temp = _copyList[rowindex - 1]
      _copyList[rowindex - 1] = _copyList[rowindex]
      _copyList[rowindex] = temp
      this.self_list = _copyList
      console.log(this.$refs.list.$refs.list.toggleRowSelection)
      this.$nextTick(() => {
        this.$refs.list.$refs.list.toggleRowSelection(_copyList[rowindex - 1], true)
      })
    },
    // 下移行
    downRow(row) {
      if (!row) return this.$alert('提示', '请选择要移动的媒体')
      if (row instanceof Array) return this.$alert('提示', '此操作不支持多选')
      let rowindex = this.self_list.findIndex(item => item._id === row._id)
      if (rowindex < 0 || rowindex === (this.self_list.length - 1)) return
      let _copyList = [].concat(this.self_list)
      let temp = _copyList[rowindex + 1]
      _copyList[rowindex + 1] = _copyList[rowindex]
      _copyList[rowindex] = temp
      this.self_list = _copyList
      this.$nextTick(() => {
        this.$refs.list.$refs.list.toggleRowSelection(_copyList[rowindex + 1], true)
      })
    },
    // 替换行
    switchRow(info, row) {
      if (!row) return this.$alert('提示', '请选择被替换的媒体')
      if (!info) return this.$alert('提示', '请选择媒体资源')
      if (row instanceof Array || (info instanceof Array && info.length > 1)) return this.$alert('提示', '此操作不支持多选')
      let rowindex = this.self_list.findIndex(item => item._id === row._id)
      if (rowindex < 0) return
      if (!info) {
        this.removeRow(row)
      } else {
        let _copyList = [].concat(this.self_list)
        _copyList[rowindex] = this._wrapFileInfo(info instanceof Array ? info[0] : info)
        this.self_list = _copyList
        this.listTimeSet(this.mode)
      }
    }
  }
}
